//
//  KFDMessageViewController.m
//  KFD
//
//  Created by Lucky Luke on 8/15/15.
//  Copyright (c) 2015 kfd. All rights reserved.
//

#import "KFDMessageViewController.h"
#import "KFDClient.h"
#import "JTToast.h"
#import "ui_config.h"
#import "JRTXLinearLayout.h"

@interface KFDMessageViewController ()

@end

@implementation KFDMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=self.message.title;
    self.view.backgroundColor=KFD_WHITE;
    KFDClient* client=[KFDClient sharedClient];
    [client checkoutMessageWithId:self.message.id successHandler:^(KFDMessage* message){
        dispatch_async(dispatch_get_main_queue(), ^{
            JRTXLinearLayout* layout=[[JRTXLinearLayout alloc] initWithFrame:CGRectMake(20, 100, SCREEN_WIDTH-40, 0)];
            UILabel* contentLabel=[[UILabel alloc] init];
            contentLabel.text=message.content;
            [layout appendLabel:contentLabel widthLimit:SCREEN_WIDTH-40 alignment:JRTXLinearLayoutAlignmentLeft];
            [self.view addSubview:layout.view];
        });
    } failureHandler:^(KFDResponseBody *response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
        });
    }];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
